package com.lonni.rpc.core.loadbalance;

import com.lonni.rpc.core.model.RpcRequest;
import com.lonni.rpc.core.model.ServiceAddress;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * 负债均衡抽象
 *
 * @author: lonni
 * @CreateTime: 2024-06-27  20:58
 * @Description:
 */
public abstract class AbstractLoadBalance implements BaseLoadbalance {

    @Override
    public ServiceAddress getServiceAddress(List<ServiceAddress> uris, RpcRequest request) {
        if (CollectionUtils.isEmpty(uris)) {
            return null;
        }
        if (uris.size() == 1) {
            return uris.get(0);
        }
        return doSelectAddress(uris, request);
    }

    /**
     * 查询地址
     *
     * @param uris
     * @param request
     * @return
     */
    public abstract ServiceAddress doSelectAddress(List<ServiceAddress> uris, RpcRequest request);

}
